package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.FacilitiesDao;
import cn.topevery.garden.dal.dao.ToiletsDao;
import cn.topevery.garden.dal.util.GeometryExtHelper;
import cn.topevery.garden.entity.po.Facilities;
import cn.topevery.garden.entity.po.FacilitiesFile;
import cn.topevery.garden.entity.po.Toilets;
import cn.topevery.garden.entity.po.ToiletsFile;
import cn.topevery.garden.entity.query.ToiletsQuery;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Service
public class ToiletsLogic extends BaseLogic<Toilets> {
    @Autowired
    ToiletsDao toiletsDao;

    @Autowired
    private FacilitiesDao facilitiesDao;

    @Autowired
    private FacilitiesFileLogic fileLogic;

    @Override
    public BaseEntityDao<Toilets, Integer> getDao() {
        return toiletsDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<Toilets> listPage(ToiletsQuery query) {
        return toiletsDao.listPage(query);
    }

    public  boolean saveOrUpdate(String userId, Toilets entity){
        // 保存设施并返回设施ID
        Integer pid = saveFacilities(userId, entity);

        entity.setFacilitiesId(pid);
        toiletsDao.saveOrUpdate(userId, entity);
        return true;
    }

    /**
     * 描述 保存设施.
     *
     * @param userId the user id
     * @param dto    the dto
     * @return the integer
     * @author wenzhao.lin
     * @created 2021 -08-31 16:45:34 Save facilities integer.
     */
    private Integer saveFacilities(String userId, Toilets dto){
        Facilities facilities = new Facilities();
        facilities.setId(dto.getFacilitiesId());
        facilities.setCode(dto.getCode());
        facilities.setName(dto.getName());
        facilities.setParkId(dto.getParkId());
        facilities.setParkName(dto.getParkName());
        facilities.setExtentId(dto.getExtentId());
        facilities.setExtentName(dto.getExtentName());
        facilities.setTypeId("4ccc6c9a-80ae-4c03-be6d-23b22e13bb09");
        facilities.setTypeName("公厕");
        facilities.setAddress(dto.getAddress());
        facilities.setStatus((short) 1);
        facilities.setDesc(dto.getDescs());
        facilities.setGeomType(0);
        List<PointDto> pointDtoList = new ArrayList<>();
        if(dto.getLngBd09() != null && dto.getLatBd09()!= null){
            pointDtoList.add(new PointDto(dto.getLngBd09(), dto.getLatBd09()));
            facilities.setGeom(GeometryHelper.getGeometry(GeometryExtHelper.PointToWkt(pointDtoList)));
        }
        facilitiesDao.saveOrUpdate(userId, facilities);
        if (dto.getFacilitiesId() != null) {
            fileLogic.delByMainId(userId, dto.getFacilitiesId());
        }
        if (dto.getFileList() != null && !dto.getFileList().isEmpty()) {
            for (ToiletsFile file : dto.getFileList()) {
                FacilitiesFile facilitiesFile = new FacilitiesFile();

                facilitiesFile.setMainId(facilities.getId());
                facilitiesFile.setFileId(file.getFileId());
                facilitiesFile.setFileName(file.getFileName());
                facilitiesFile.setFileType(file.getFileType());
                facilitiesFile.setFileTypeId(file.getFileTypeId());

                fileLogic.saveOrUpdate(userId, facilitiesFile);
            }
        }
        return facilities.getId();
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        toiletsDao.delete(userId, ids);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    public boolean isExit(String fieldName, String value){
        return toiletsDao.isExit(fieldName, value);
    }
}
